package dao.impl;

import dao.StaffInfoDao;
import entity.StaffInfo;
import util.DBConn;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * StaffInfoDao接口的实现 - 基于原生JDBC实现
 * @author gwf
 * @date 2025-02-24
 */
public class StaffInfoDaoImpl implements StaffInfoDao {

    @Override
    public List<StaffInfo> list() {
        String sql = "select id,name,age,phone,sta_pos,add_time,update_time from staffs";
        List<StaffInfo> staffInfos = DBConn.list(sql,StaffInfo.class);
        return staffInfos;
    }

    @Override
    public StaffInfo one(int id) {
        String sql = "select name,age,phone,sta_pos,add_time,update_time from staffs where id=?";
        StaffInfo one = DBConn.one(sql,StaffInfo.class,id);
        return one;
    }

    @Override
    public int add(StaffInfo staffInfo) {
        String sql = "insert into staffs(name,age,phone,sta_pos) values(?,?,?,?)";
        int update = DBConn.update(sql,staffInfo.getName(),staffInfo.getAge(),staffInfo.getPhone(),staffInfo.getStaPos());
        return update;
    }

    @Override
    public int del(int id) {
        String sql = "delete from staffs where id=?";
        int update = DBConn.update(sql,id);
        return update;
    }

    @Override
    public int modify(StaffInfo staffInfo) {
        String sql = "update staffs set name=?,age=?,phone=?,sta_pos=?";
        int update = DBConn.update(sql,staffInfo.getName(),staffInfo.getAge(),staffInfo.getPhone(),staffInfo.getStaPos());
        return update;
    }

    @Override
    public List<StaffInfo> selectPage(int currentPage, int pageSize) {
        List<StaffInfo> list = new ArrayList<>();
        String sql ="select * from staffs limit ?,?";
        Connection conn = DBConn.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);

            int limit = (currentPage-1)*pageSize;
            ps.setInt(1,limit);
            ps.setInt(2,pageSize);

             rs = ps.executeQuery();

            while (rs.next()){
                //1.读取各列数据
                int id = rs.getInt(1);
                String name = rs.getString(2);
                int age = rs.getInt(3);
                String phone = rs.getString(4);
                String staPos = rs.getString(5);

                //把java.sql.Timestamp转换成java.time.LocalDateTime
                LocalDateTime addTime = rs.getTimestamp(6).toLocalDateTime();
                LocalDateTime updateTime = rs.getTimestamp(7).toLocalDateTime();

                //2.实例化实体对象
                StaffInfo staffInfo = new StaffInfo();
                staffInfo.setId(id);
                staffInfo.setName(name);
                staffInfo.setAge(age);
                staffInfo.setPhone(phone);
                staffInfo.setStaPos(staPos);
                staffInfo.setAddTime(addTime);
                staffInfo.setUpdateTime(updateTime);

                //3.把实体对象添加到list中
                list.add(staffInfo);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBConn.close(rs,ps,conn);
        }
        return list;
    }
}
